$(document).ready(function(e) {
	if($G['func']=='init' && $G['part']!='params'){
		if($G['part'] == 'market'){
			$.ajax({
				url: 'https://api.bosscms.net/rest/user/test.php?auth=free',
				error: function(){
					window.location.href = $.mpf('template','template','init');
				}
			});
		}
		$.post('https://api.bosscms.net/rest/user/identity.php?auth=free',{'user_sequence':$G['user']},function(data){
			$('body').prepend( sethtml.identity(data) );

			$('.market>.filter>dl>dd>form').after(
				'<a class="collect '+($.request('collect')=='list'?'red':'')+'"'+
				(data.money?'href="'+$.mpf('template','market','init',($.request('collect')=='list'?null:{'collect':'list'}))+'"':'href="javascript:_alert(\'请先登录官方账号\');"')+
				'><i class="fa fa-star"></i>我的收藏</a>'
			);
			if($G['part'] == 'market'){
				var url = $.paramobj('https://api.bosscms.net/rest/user/templates.php?auth=free',{
					'pages': $.request('pages'),
					'divide': $.request('divide'),
					'collect': $.request('collect'),
					'search': $.request('search'),
					'names': $('.market>.list').attr('names')||null
				}), param = window.location.href.match(/param,\d+/g);
				for(i in param){
					url = $.params(url,param[i],$.request(param[i]));
				}	
				$.post(url,{'user_sequence':$G['user']},function(res){
					html = '';
					for(k in res.list){
						v = res.list[k];
						html += '<li><aside><p class="img"><img src="'+v['image']+'" alt="'+v['name']+'" /></p>'+
								'<p class="name"><a href="'+v['link']+'" target="_blank" class="name"><u class="green">'+v['name']+'</u>'+v['title']+'</a></p>'+
								'<p class="text"><span class="price">价格：';
						if(v['groups'] == 0){
							html += '免费';
						}else if(v['price']==0){
							html += v['remark'];
						}else{
							html += '<i class="fa fa-rmb money">'+v['price']+'</i> / '+v['buy_duration'];
							if(v['remark']){
								html += '<a href="javascript:_alert(\''+v['remark']+'\',\'gold\');" color="red" class="preferential">[优惠]</a>';
							}
						}
						html +=	'</span></p><p class="btn">';
						if(v['groups']==0 || v['buyed']){
							html +=	'<a class="btnfa green" ';
							if(data.money){
								html +=	'url="'+$.mpf('template','market','install',{'name':v['name']})+'" install="'+v['name']+'" groups="'+v['groups']+'"';
							}else{
								html +=	'href="javascript:_alert(\'请先登录官方账号\');"';
							}
							html +=	'><em class="fa fa-cloud-download" title="安装"></em></a> ';
						}else{
							html +=	'<a class="btnfa green buy" ';
							if(data.money){
								if(v['price']==0){
									html += 'href="javascript:_alert(\''+v['remark']+'\');"';
								}else{
									html +=	'easy="nofull" width="580" height="420" name="购买 “'+v['name']+'” 模板" url="'+$.mpf('template','market','info',{'id':v['id'],'name':v['name']})+'"';
								}
							}else{
								html +=	'href="javascript:_alert(\'请先登录官方账号\');"';
							}
							html +=	'><em class="fa fa-pencil" title="购买"></em></a> ';
						}
						if(v['collect']){
							html +=	'<a class="btnfa red" ';
							if(data.money){
								html +=	'href="'+$.mpf('template','market','collect',{'id':v['id']})+'"';
							}else{
								html +=	'href="javascript:_alert(\'请先登录官方账号\');"';
							}
							html +=	'><em class="fa fa-star" title="已收藏"></em></a> ';
						}else{
							html +=	'<a class="btnfa blue" ';
							if(data.money){
								html +=	'href="'+$.mpf('template','market','collect',{'id':v['id']})+'"';
							}else{
								html +=	'href="javascript:_alert(\'请先登录官方账号\');"';
							}
							html +=	'><em class="fa fa-star-o" title="收藏"></em></a> ';
						}
						html +=	'<a class="btnfa red" href="'+v['demo']+'" target="_blank"><em class="fa fa-eye" title="演示"></em></a></p></aside></li>';
					}
					$('.market>.list>ul').html( html );
					$('.market>.pages>ol').html( sethtml.pages(res.pages, res.total, -1) );
				},'json');
			}else if($G['part'] == 'template'){
				$.post($.paramobj('https://api.bosscms.net/rest/user/update.php?auth=free',{
					'type': 'templates',
					'names': $('.market>.list').attr('names')
				}),{'user_sequence':$G['user']},function(res){
					for(k in res){
						if(res[k] > $('img[alt="'+k+'"]').attr('version')){
							$('img[alt="'+k+'"]').parents('li').find('p.btn').prepend(
								'<a class="btnfa green" '+
								(data.money?'href="'+$.mpf('template','market','install',{'name':k,'update':res[k]})+'"':'href="javascript:_alert(\'请先登录官方账号\');"')+
								'><em class="fa fa-cloud-upload" title="升级"></em></a>'
							);
						}
					}
				},'json');
			}
		},'json');
		$.get($.params('https://api.bosscms.net/rest/user/field.php?auth=free','divide',$.request('divide')),function(res){
			html =  '<li class="'+(!$.request('divide')&&$G['part']!='template'?'on':'')+'"><a href="'+$.mpf('template','market','init')+'">全部</a></li>'+
					'<li class="'+($G['part']=='template'?'on':'')+'"><a href="'+$.mpf('template','template','init')+'">已安装</a></li>';
			for(k in res.templates.divide){
				html += '<li class="'+($.request('divide')==k?'on':'')+'"><a href="'+$.mpf('template','market','init',{'divide':k})+'">'+res.templates.divide[k]+'</a></li>';
			}
			$('.market>.cut>ul').html(html);
			html = '';
			for(k in res.templates.complex){
				v = res.templates.complex[k];
				html += '<dl><dt>'+v['title']+'：</dt><dd class="param">'+
						'<a class="'+(!$.request(v['name'])?'on':'')+'" href="'+$.params($.paramobj(window.location.href,{'part':'market','pages':null}),v['name'],null)+'">全部</a>';
				for(k2 in v['param']){
					s = v['param'][k2];
					html += '<a class="'+($.request(v['name'])==s?'on':'')+'" href="'+$.params($.paramobj(window.location.href,{'part':'market','pages':null}),v['name'],encodeURI(s))+'">'+s+'</a>';
				}
				html += '</dd></dl>';
			}
			$('.market>.filter').prepend(html);
		},'json');
	}


	$(document).on('change','input[name="pi"][type="radio"]',function(){
		$('dl.nid').removeClass('on').find('label input[value=0]').click();
		$('dl.nid[nid="'+$(this).val()+'"]').addClass('on');
		price = $(this).attr('price');
		$('label.price i').html(price);
		$('label.price input').val(price).attr('price',price);
	}).on('change','input[name^="coupon"][type="radio"]',function(){
		if($(this).prop('checked')){
			the = $('label.price input');
			if(!(inum = $(this).parent('label').find('.coupon u').html())){
				inum = 0;
			}
			price = the.attr('price')-inum;
			price = price>0?price:0;
			$('label.price i').html(price);
			the.val(price);
		}	
	}).on('change','input[name="pay"][type="radio"]',function(){
		if($(this).val()=='balance'){
			$('dl.password').removeAttr('hide');
			$(window).scrollTop(999);
		}else{
			$('dl.password').attr('hide','hide');
		}
	}).on('submit','form#buy',function(){
		if(!$('input[name="protocol"][type="checkbox"]').is(':checked')){
			_alert('请阅读协议并点击确认');
			return false;
		}
		if($('input[name="pay"][type="radio"]:checked').val()=='balance' && $('dl.password input[name="password"]').val()==''){
			_alert('请填写官方账号密码');
			$('dl.password input[name="password"]').focus();
			return false;
		}
	}).on('click','a[url][install]',function(){
		iname = $(this).attr('install');
		if($(this).attr('groups')==0){
			window.location.href = $(this).attr('url');
		}else{
			if($('section.easy.install').length==0){
				$('body').append('<section class="easy install">'+
				  '<div class="window">'+
					'<div class="icon">'+
					  '<span class="close "><em class="fa fa-times"></em></span>'+
					'</div>'+
					'<div class="move">授权域名</div>'+
					'<iframe></iframe>'+
				  '</div>'+
				'</section>');
			}
			$('section.easy.install div.window')
			  .css({'width':680,'height':440,'left':$('section.easy.install').width()/2-340,'top':$('section.easy.install').height()/2-220,})
			  .find('iframe').attr('src',$.mpf('template','market','inst',{name:iname}));
		}
	});
	if($('.order a.install').length>0){
		$('.order a.install').attr('href', $('.order a.install').attr('href').replace('referer=[url]','referer='+encodeURIComponent(window.parent.window.location.href)));
	}
	if($('section.main.bind').length>0){
		$('section.easy.install',window.parent.document).addClass('active');
		$(document).on('click','section.main.bind a.auth[url]',function(){
			if(confirm('请确认该软件产品安装到本站')){
				window.location.href = $(this).attr('url');
			}
		});
	}
});